$('#calendar').calendar();
$('#calendar').calendar().on("clickCell.zui.calendar", function(event) {
    console.log(event.timeStamp);

});
$(".form-datetime").datetimepicker(
    {
        weekStart: 1,
        todayBtn:  1,
        autoclose: 1,
        todayHighlight: 1,
        startView: 2,
        forceParse: 0,
        showMeridian: 1,
        format: "yyyy-mm-dd hh"
    });


var list = [];

function DateTimeObject(dateTime){
    this.dateTime = dateTime;
}

function getTime(){
    let dateTime = $('#data-time-inp').val();
    let arr = dateTime.split("-");
    let year = parseInt(arr[0]);
    let month = parseInt(arr[1]);
    let day = parseInt(arr[2].split(" ")[0]);
    let hour = parseInt(arr[2].split(" ")[1]);
    let dateTime2 = dateTime.split(" ");
    let str = dateTime2[0] + "-" + dateTime2[1];
    var myDate = new Date;
    var cyear = myDate.getFullYear(); //获取当前年
    if (cyear > year){
        alert("排班的年份不能小与当前年份！");
        return;
    }
    var cmon = myDate.getMonth() + 1; //获取当前月
    if (cmon > month){
        alert("排班的月份不能小与当前月份！");
        return;
    }
    var cdate = myDate.getDate(); //获取当前日
    if (cdate > day){
        alert("排班的日期不能小与当前日期！");
        return;
    }
    var h = myDate.getHours();//获取当前小时数(0-23)
    if (h > hour){
        alert("排班的时间不能小与当前时间！");
        return;
    }
    for (let i = 0; i < list2.length; i++) {
        console.log(list2[i])
        if (str == list2[i]){
            alert("该时间段已存在不能添加！");
            return;
        }
    }
    list.push(new DateTimeObject(dateTime));
    /* 获取日历对象实例 */
    var calendar = $('#calendar').data('zui.calendar');
    var newEvent = {
                        title: '空闲',
                        desc: '工作时长一个小时',
                        start: new Date(year + 1 -1,month -1,day + 1-1,hour+1-1,0,0),
                        end: new Date(year+1-1,month-1,day +1 -1,hour + 1,0,0)
                    };
    calendar.addEvents(newEvent);
    $('#data-time-inp').val("");
}

function subBtn(){
    console.log(list);
    let parentElement = parent.document;
    let username = parentElement.getElementById('top-manager1').innerText
    console.log(username);
    $.ajax({
        url:config.url + "/cons/arrange",
        dataType:'json',
        xhrFields: {
            withCredentials: true
        },
        data:{
            list:JSON.stringify(list),
            username:username
        },
        crossDomain: true,
        type:'POST',
        success:function(resp) {
            alert(resp.msg);
        }
    });
}
getDateTimeInfo();
function getDateTimeInfo(){
    let parentElement = parent.document;
    let username = parentElement.getElementById('top-manager1').innerText
    $.ajax({
        url:config.url + "/cons/timeinfo",
        dataType:'json',
        xhrFields: {
            withCredentials: true
        },
        data:{
            username:username
        },
        crossDomain: true,
        type:'GET',
        success:function(resp) {
            var temp = JSON.parse(resp.data);
            renderDate(temp);
        }
    });
}
var list2 = [];
function renderDate(arr){
    for (let i = 0; i < arr.length; i++) {
        var str = arr[i].dateTime
        let dateAndTime = str.split("-");
        console.log(dateAndTime)
        let year = parseInt(dateAndTime[0]);
        let month = parseInt(dateAndTime[1]);
        let day = parseInt(dateAndTime[2]);
        let hour = parseInt(dateAndTime[3]);
        list2.push(str);
        /* 获取日历对象实例 */
        var calendar = $('#calendar').data('zui.calendar');
        var newEvent = {
            title: '空闲',
            desc: '工作时长一个小时',
            start: new Date(year + 1 -1,month -1,day + 1-1,hour+1-1,0,0),
            end: new Date(year + 1 -1,month -1,day + 1-1,hour + 1,0,0)
        };
        calendar.addEvents(newEvent);
        $('#data-time-inp').val("");
    }

}